package com.llw.notify.notification;

import android.app.Notification;
import android.content.ComponentName;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import android.util.Log;


import com.llw.notify.setting.SettingsManager;
import com.llw.notify.util.HTMLMarker;
import com.llw.notify.util.Utils;

public class NotifyService extends NotificationListenerService {
    private final String TAG = getClass().getSimpleName();

    @Override
    public void onListenerConnected() {
        super.onListenerConnected();
        Log.e(TAG, "侦听器已连接");
    }

    /**
     * 检查该通知是否被禁止转发
     *
     * @param packageName 发出通知应用的包名
     * @param notification 通知
     * @return 若是黑名单应用或者通知内容包含屏蔽词则返回 false，否则返回 true
     */
    private boolean isBlocked(final String packageName, final Notification notification) {
        // 检查是否是被屏蔽应用
        if (SettingsManager.isInBlackApps(packageName)) {
            Log.d(TAG, "黑名单应用：" + packageName);
            return true;
        }
        // 检查是否包含屏蔽词
        final String blackWord = SettingsManager.hasBlackWords(NotificationExtractor.extractRaw(notification));
        if (blackWord != null) {
            Log.d(TAG, "通知包含屏蔽词：" + blackWord);
            return true;
        }
        return false;
    }

    /**
     * 发布通知
     *
     * @param sbn 状态栏通知
     */
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        if (!SettingsManager.isEmailInfoSetup()) {
            Log.e(TAG, "邮件地址没有配置，转发不能");
            return;
        }

        if (!SettingsManager.isSyncSwitchOn()) {
            Log.i(TAG, "转发开关被关闭，放弃转发");
            return;
        }

        final String packageName = sbn.getPackageName();
        final String appName = Utils.getAppNameByPackageName(packageName);
        final Notification notification = sbn.getNotification();
        if (notification == null) {
            Log.i(TAG, "包名：" + packageName + "，应用名：" + appName + "：找不到通知内容，放弃转发");
            return;
        }

        // 检查通知是否被屏蔽
        if (isBlocked(packageName, notification) == true) {
            Log.d(TAG, "包名：" + packageName + "，应用名：" + appName + "：通知被屏蔽，放弃转发");
            return;
        }

        final String notificationBody = TextUtils.join(HTMLMarker.LF, NotificationExtractor.extract(notification));
        Log.d(TAG, "通知内容：" + HTMLMarker.LF + notificationBody);
        if (TextUtils.isEmpty(notificationBody)) {
            Log.i(TAG, "通知内容为空，放弃转发");
            return;
        }

        new EmailSynchronizer(
                packageName,
                NotificationExtractor.extractKey(notification, NotificationExtractor.Content.TITLE),
                notificationBody,
                sbn.getPostTime()
        ).start();
    }

    /**
     * 通知已删除
     *
     * @param sbn 状态栏通知
     */
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i(TAG, "通知被移除：" + Utils.getAppNameByPackageName(sbn.getPackageName()));
    }

    /**
     * 监听断开
     */
    @Override
    public void onListenerDisconnected() {
        Log.i(TAG, "侦听器断连，重连中");
        // 通知侦听器断开连接 - 请求重新绑定
        requestRebind(new ComponentName(this, NotificationListenerService.class));
    }
}
